package com.nowcoder.community.d_controller.Lanjieqi;


import com.nowcoder.community.a_entity.User;
import com.nowcoder.community.c_service.MessageService;
import com.nowcoder.community.z_util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>——————————————————————————————————————————————-————
 * <p> MessageInterceptor：首页header复用，里面的消息个数，每次都要count*，拦截器实现加入到对应RM代码内（内！细品！）
 * <p>——————————————————————————————————————————————-————
 */
@Component
public class MessageInterceptor implements HandlerInterceptor{
    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private MessageService messageService;


    /**
     * <p>——————————————————————————————————————————————-————
     * <p> ：这个是默认方法，要自己写出来【h：这个补入cofig中标明的RM前面】
     * <p>——————————————————————————————————————————————-————
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = hostHolder.getUser();
        if (user != null && modelAndView != null) {
            int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
            int noticeUnreadCount = messageService.findNoticeUnreadCount(user.getId(), null);
            modelAndView.addObject("allUnreadCount", letterUnreadCount + noticeUnreadCount);
        }
    }

}
